Requires Scripting PRO
The BackgroundKeeper API provides control over background keep-alive behavior in the Scripting App, allowing scripts to extend their runtime after the app transitions to the background. This can be useful for maintaining ongoing operations, such as network connections or active processes, for a limited period while the app is not in the foreground.
Availability: This API is only available when the script is running in the main app (
Script.env === "index"). Use it responsibly, as prolonged background execution may increase power consumption.
When the app moves to the background (due to a background event such as a system-triggered state change), you can call BackgroundKeeper.keepAlive() to request that the app continue running for a limited duration.
When the app returns to the foreground, you should call BackgroundKeeper.stopKeepAlive() to release background resources.
Multiple scripts can request background keep-alive at the same time. Internally, the system maintains a keep-alive request queue:
keepAlive() adds the calling script to the queue.stopKeepAlive() removes it.Note: Even with keep-alive enabled, iOS may still terminate the app under conditions such as high memory usage or strict power constraints.
BackgroundKeeperisActive: Promise<boolean>Returns a promise that resolves to a boolean value indicating whether the keep-alive process is currently active.
Example:
keepAlive(): Promise<boolean>Starts the background keep-alive process.
true.true.false.Returns:
Promise<boolean> — Indicates whether the keep-alive process was successfully started.
Example:
stopKeepAlive(): Promise<void>Stops the keep-alive request for the current script. This does not guarantee that the entire keep-alive process will stop immediately, as other scripts may still have active requests.
Returns:
Promise<void> — Resolves when the stop request is processed.
Example:
BackgroundKeeper for indefinite background execution. The system may still suspend or terminate the app.